package com.hit.server.comm;

import cn.hutool.extra.spring.SpringUtil;
import com.hit.comm.dto.CommWrapperDTO;
import com.hit.comm.dto.RegisterInfoReq;
import com.hit.comm.type.CommTypeEnum;
import com.hit.comm.utils.ProtostuffUtils;
import com.hit.server.api.service.ServerMsgHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kehanjiang
 * @date 2025/03/24
 */
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<CommWrapperDTO> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CommWrapperDTO commWrapperDTO) throws Exception {
        try {
            ServerMsgHandler serverMsgHandler = SpringUtil.getBean(ServerMsgHandler.class);
            CommTypeEnum type = CommTypeEnum.getByType(commWrapperDTO.getType());
            if (type != null) {
                switch (type) {
                    case TYPE_10000:
                        RegisterInfoReq dto = ProtostuffUtils.deserialize(commWrapperDTO.getData(), RegisterInfoReq.class);
                        serverMsgHandler.registerClient(dto);
                        break;
                    default:
                        log.error("不支持的类型：{}", commWrapperDTO.getType());
                }
            }
        } catch (Exception e) {
            log.error("==> NettyServerHandler channelRead error:{}", e.getMessage());
        }
    }
}